package cn.yqx.bankshop.goods.controller;

import cn.yqx.bankshop.goods.page.PageRequest;
import cn.yqx.bankshop.goods.page.PageResult;
import cn.yqx.bankshop.goods.service.GoodSearchService;
import cn.yqx.bankshop.result.Result;
import cn.yqx.bankshop.result.ResultCode;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/search")
public class GoodsSearchController {

    private static final  String KEYWORD_KEY = "keyword" ;
    private static final String PAGE_REQUEST_KEY = "pageRequest" ;
    private static final String PRODUCT_GRADE_TWO_KEY = "productGradeTwoKey" ;
    private static final String PRODUCT_GRADE_FOUR_KEYS_KEY = "productGradeFourKeys" ;
    private static final String FORMAT_KVS_KEY = "formatKVs" ;
    private static final String FORMAT_K_KEY = "formatKey" ;
    private static final String FORMAT_V_KEY = "formatVal" ;
    private static final String KEY_STR = "key" ;
    private static final String VAL_STR = "val" ;
    private static final String ORDER_DATAS_KEY = "orderDatas" ;
    private static final String RANGE_DATAS_KEY = "rangeDatas" ;
    private static final int LIST_LENGTH = 10 ;
    @Autowired
    GoodSearchService goodSearchService ;

    @RequestMapping("/importAll")
    public String importAll(){
        try {
            goodSearchService.importAll();
        } catch ( IOException e) {
            e.printStackTrace();
        }
        return "success";
    }

    @RequestMapping(value = "/search-goods", method = RequestMethod.POST)
    public Object searchGoods(@RequestBody JSONObject json){
        String keyword = json.getString(KEYWORD_KEY) ;

        PageRequest pageRequest = JSON.parseObject(json.getJSONObject(PAGE_REQUEST_KEY).toJSONString(), PageRequest.class) ;
        JSONObject screenData = json.getJSONObject("screenData") ;

        Map<String,Object>  searchMap = new HashMap<String, Object>(10) ;

        //关键词
        searchMap.put(KEYWORD_KEY,keyword) ;
        //二级分类
        searchMap.put(PRODUCT_GRADE_TWO_KEY,screenData.getString(PRODUCT_GRADE_TWO_KEY)) ;
        //类型key
        List<String> keyList = new ArrayList<String>(LIST_LENGTH) ;
        List<String> valList = new ArrayList<String>(LIST_LENGTH) ;
        JSONArray formatKVs = screenData.getJSONArray(FORMAT_KVS_KEY) ;
        //orderDatas 排序
        JSONArray orderDatas = screenData.getJSONArray(ORDER_DATAS_KEY) ;
        List<Map<String,String>> orderDataList = new ArrayList<Map<String,String>>(LIST_LENGTH) ;
        if(orderDatas!=null && !orderDatas.isEmpty()){
            orderDatas.forEach((item)->{
                orderDataList.add((Map)item) ;
            });
            searchMap.put(ORDER_DATAS_KEY,orderDataList) ;
        }

        //rangeDatas 范围查询
        JSONArray rangeDatas = screenData.getJSONArray(RANGE_DATAS_KEY) ;
        List<Map<String,String>> rangeDataList = new ArrayList<Map<String,String>>(LIST_LENGTH) ;
        if(rangeDatas!=null && !rangeDatas.isEmpty()){
            rangeDatas.forEach((item)->{
                rangeDataList.add((Map)item) ;
            });
            searchMap.put(RANGE_DATAS_KEY,rangeDataList) ;
        }

        if(!formatKVs.isEmpty()){
            formatKVs.forEach((item)->{
                keyList.add((String) ((Map) item).get(KEY_STR)) ;
                valList.add((String) ((Map) item).get(VAL_STR)) ;
            });
            searchMap.put(FORMAT_K_KEY,keyList) ;
            searchMap.put(FORMAT_V_KEY,valList) ;
        }

        JSONArray fourKeys = screenData.getJSONArray(PRODUCT_GRADE_FOUR_KEYS_KEY) ;
        List<String> fourKeyList = JSONObject.parseArray(fourKeys.toJSONString(), String.class);
        searchMap.put(PRODUCT_GRADE_FOUR_KEYS_KEY,fourKeyList) ;
        log.info(JSON.toJSONString(searchMap));
        PageResult pageResult ;
        Result result  ;
        try{
            result = Result.success();
            pageResult =  goodSearchService.searchGoods(searchMap , pageRequest ) ;
            result.setData(pageResult);
        }catch (Exception e){
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR) ;
            log.info("error : {} " , e );
        }
        return  result;
    }

}
